'use strict';

goog.provide('Blockly.JavaScript.customs');

goog.require('Blockly.JavaScript');


Blockly.JavaScript.customs_digital_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'number_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};


Blockly.JavaScript.customs_analog_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};


Blockly.JavaScript.customs_show_highlow = function() {
  var dropdown_d_value = Blockly.JavaScript.valueToCode(this, 'd_values', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  if(dropdown_d_value=="HIGH"){
    var code = "1";
  }else{
    var code = "0";
  }
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};



Blockly.JavaScript.customs_digital_write = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  var value_p_value = Blockly.JavaScript.valueToCode(this, 'p_value', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.

  var fun_name='digital_write_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+value_pin+'", "1"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "digital_write", "params": ["'+value_pin+'", '+value_p_value+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_pin+'",'+value_p_value+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+value_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript.customs_digital_read = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.

  var fun_name='digital_read_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+value_pin+'", "0"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "digital_read", "params": ["'+value_pin+'"]},0);\n' +
    'ActionBlock.set_panel_read_data("'+value_pin+'",0);\nreturn ActionBlock.reply_data;\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+value_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'()';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript.customs_analog_write = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  var value_p_value = Blockly.JavaScript.valueToCode(this, 'p_value', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var new_pin=null;
  var pin_mode=null;
  switch (value_pin){
    case "A0":
      new_pin='14';
      pin_mode="1";
      break;
    case "A1":
      new_pin="15";
      pin_mode="1";
      break;
    case "A2":
      new_pin="16";
      pin_mode="1";
      break;
    case "A3":
      new_pin="17";
      pin_mode="1";
      break;
    case "A4":
      new_pin="18";
      pin_mode="1";
      break;
    case "A5":
      new_pin="19";
      pin_mode="1";
      break;
    default:
      new_pin=value_pin;
      pin_mode="3";
      break;
  }

  var fun_name='analog_write_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+new_pin+'", "'+pin_mode+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", '+value_p_value+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_pin+'",'+value_p_value+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript.customs_analog_read = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var new_pin=null;
  var pin_mode=null;
  switch (value_pin){
    case "A0":
      new_pin='0';
      pin_mode="2";
      break;
    case "A1":
      new_pin="1";
      pin_mode="2";
      break;
    case "A2":
      new_pin="2";
      pin_mode="2";
      break;
    case "A3":
      new_pin="3";
      pin_mode="2";
      break;
    case "A4":
      new_pin="4";
      pin_mode="2";
      break;
    case "A5":
      new_pin="5";
      pin_mode="2";
      break;
    default:
      new_pin=value_pin;
      pin_mode="2";
      break;
  }

  var fun_name='analog_read_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+new_pin+'", "'+pin_mode+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "analog_read", "params": ["'+new_pin+'"]},0);\n' +
    'ActionBlock.set_panel_read_data("'+value_pin+'",0);\nreturn ActionBlock.reply_data;\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'()';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};


Blockly.JavaScript.customs_pulse_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Roller_switch_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Key_switch_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Action_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Photosensitive_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Magnetic_switch_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Touch_switch_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Threshold_trigger_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_potentiometer_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Pressure_sensing_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Photosensitive_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code,"order"];
};

Blockly.JavaScript.customs_Sound_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Grayscale_detection_module_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Remote_sensing_module_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Ultrasonic_range_finder_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Sliding_rheostat_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};

Blockly.JavaScript.customs_Infrared_detector_pin = function() {
  // Numeric value.
  var code = Blockly.JavaScript.valueToCode(this, 'string_values', Blockly.JavaScript.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.JavaScript.ORDER_UNARY_PREFIX : Blockly.JavaScript.ORDER_ATOMIC;
  return [code, order];
};


Blockly.JavaScript.customs_motor_m = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript.customs_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript.customs_servo_motor_single = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
  var dropdown_motor_m = Blockly.JavaScript.valueToCode(this, 'MOTOR_M', Blockly.JavaScript.ORDER_ATOMIC);
  var dropdown_direction_set = Blockly.JavaScript.valueToCode(this, 'DIRECTION_SET', Blockly.JavaScript.ORDER_ATOMIC);
  var number_speed_set_val = Blockly.JavaScript.valueToCode(this, 'SPEED_SET_VAL', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
   var PIN_list=[];
    var min_speed=0;  //其中最小的输出
    if(dropdown_motor_m=='M1'){// 第一个电机
        if(dropdown_direction_set=='F'){  //正转
            PIN_list[0]='5';
            PIN_list[1]='3';
        }
        else{
            PIN_list[0]='3';
            PIN_list[1]='5';
        }
    }
    else{
        if(dropdown_direction_set=='F') {  //正转
            PIN_list[0] = '9';
            PIN_list[1] = '6';
        }
        else{
            PIN_list[0] = '6';
            PIN_list[1] = '9';
        }
    }
  var fun_name='one_motor_run_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "motor_config", "params": ["'+PIN_list[0]+'", "'+PIN_list[1]+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "one_motor_run", "params": [["'+PIN_list[0]+'","'+PIN_list[1]+'"],['+number_speed_set_val+','+min_speed+']]},0);\n' +
    'ActionBlock.set_panel_write_data("'+PIN_list[0]+'",'+number_speed_set_val+',0);\n' +
    'ActionBlock.set_panel_write_data("'+PIN_list[1]+'",'+min_speed+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+PIN_list[0]+'_'+PIN_list[1]]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript.customs_two_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript.customs_two_motor_speed = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript.customs_servo_motor_two = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
    //D5 D3电机在左边
    //D9 D6电机在右边
  var dropdown_motor_m = Blockly.JavaScript.valueToCode(this, 'MOTOR_M', Blockly.JavaScript.ORDER_ATOMIC);
  var dropdown_speed_set = Blockly.JavaScript.valueToCode(this, 'SPEED_SET', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var pin_list=['5','3','9','6'];
  var pin_value=[];
  var init_pin_value='0';
  switch(dropdown_motor_m){
      case 'F':
          pin_value=[dropdown_speed_set,init_pin_value,dropdown_speed_set,init_pin_value];
          break;
      case 'B':
          pin_value=[init_pin_value,dropdown_speed_set,init_pin_value,dropdown_speed_set];
          break;
      case 'L':
          var min_set=0;
          pin_value=[dropdown_speed_set,init_pin_value,min_set,init_pin_value];
          break;
      case 'R':
          var min_set=0;
          pin_value=[min_set,init_pin_value,dropdown_speed_set,init_pin_value];
          break;
  }

  var fun_name='two_motor_run_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "motor_config", "params": ["'+pin_list[0]+'", "'+pin_list[1]+'","'+pin_list[2]+'","'+pin_list[3]+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "two_motor_run", "params": [["'+pin_list[0]+'","'+pin_list[1]+'","'+pin_list[2]+'","'+pin_list[3]+'"],['+pin_value[0]+','+pin_value[1]+','+pin_value[2]+','+pin_value[3]+']]},0);\n' +
    'ActionBlock.set_panel_write_data("'+pin_list[0]+'",'+pin_value[0]+',0);\n' +
    'ActionBlock.set_panel_write_data("'+pin_list[1]+'",'+pin_value[1]+',0);\n' +
    'ActionBlock.set_panel_write_data("'+pin_list[2]+'",'+pin_value[2]+',0);\n' +
    'ActionBlock.set_panel_write_data("'+pin_list[3]+'",'+pin_value[3]+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+pin_list[0]+'_'+pin_list[1]]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript.customs_oled_single = function() {
  var value_display_text = Blockly.JavaScript.valueToCode(this, 'DISPLAY_TEXT', Blockly.JavaScript.ORDER_ATOMIC);
  var value_row_x = Blockly.JavaScript.valueToCode(this, 'ROW_X', Blockly.JavaScript.ORDER_ATOMIC);
  var value_column_y = Blockly.JavaScript.valueToCode(this, 'COLUMN_Y', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var code='';
  return code;
};


Blockly.JavaScript.customs_oled_display = function() {
  var value_text_one = Blockly.JavaScript.valueToCode(this, 'TEXT_ONE', Blockly.JavaScript.ORDER_ATOMIC);
  var value_text_two = Blockly.JavaScript.valueToCode(this, 'TEXT_TWO', Blockly.JavaScript.ORDER_ATOMIC);
  var value_text_three = Blockly.JavaScript.valueToCode(this, 'TEXT_THREE', Blockly.JavaScript.ORDER_ATOMIC);
  var value_text_four = Blockly.JavaScript.valueToCode(this, 'TEXT_FOUR', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var code='';
  return code;
};


Blockly.JavaScript.customs_tone_rhythm = function() {
  var dropdown_rhythm = this.getFieldValue('number_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_rhythm;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript.customs_play_sound = function() {
  var value_tone_play = Blockly.JavaScript.valueToCode(this, 'tone_play', Blockly.JavaScript.ORDER_ATOMIC);
  var value_frequency = Blockly.JavaScript.valueToCode(this, 'frequency', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var fun_name='play_sound_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "play_tone", "params": ["'+value_tone_play+'","TONE_TONE","'+value_frequency+'","0"]},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_tone_play+'",'+value_frequency+',0);\n}';
  Blockly.JavaScript.definitions_[fun_name]=pin_write;
  var code = fun_name+'();';
  return code;
};


Blockly.JavaScript.customs_play_sound_duration = function() {
  var value_tone_play = Blockly.JavaScript.valueToCode(this, 'tone_play', Blockly.JavaScript.ORDER_ATOMIC);
  var value_frequency = Blockly.JavaScript.valueToCode(this, 'frequency', Blockly.JavaScript.ORDER_ATOMIC);
  var value_otime = Blockly.JavaScript.valueToCode(this, 'otime', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.

  var fun_name='play_sound_duration_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "play_tone", "params": ["'+value_tone_play+'","TONE_TONE","'+value_frequency+'",'+value_otime*1000+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_tone_play+'",'+value_frequency+',0);\n' +
    'ActionBlock.set_panel_write_data("'+value_tone_play+'","0",'+value_otime*1000+');\n}';
  Blockly.JavaScript.definitions_[fun_name]=pin_write;
  var code = fun_name+'();';
  return code;
};



Blockly.JavaScript.customs_stop_sound = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'PIN', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var fun_name='stop_sound_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "play_tone", "params": ["'+value_pin+'","TONE_NO_TONE","0","0"]},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_pin+'","0",0);\n}';
  Blockly.JavaScript.definitions_[fun_name]=pin_write;
  var code = fun_name+'();';
  return code;
};


Blockly.JavaScript.customs_temperature_read = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};


Blockly.JavaScript.customs_humidity_read = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};
